/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pantallas;

import clases.MuebleEstructura;
import clases.Pieza;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author DanielRH
 */
public class PantallaPiezas extends javax.swing.JFrame {

    /**
     * Creates new form PantallaPiezas
     */
    public PantallaPiezas() {
        initComponents();
        cargarMouseListener();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAgregarPiezas = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPiezas = new javax.swing.JTable();
        btnSiguiente = new javax.swing.JButton();
        btnAtras = new javax.swing.JButton();
        btnEliminarPieza = new javax.swing.JButton();
        txtPiezaSeleccionada = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        btnAgregarPiezas.setText("Agregar Pieza");
        btnAgregarPiezas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarPiezasActionPerformed(evt);
            }
        });

        tablaPiezas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablaPiezas);

        btnSiguiente.setText("Siguiente");
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        btnAtras.setText("Atras");
        btnAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtrasActionPerformed(evt);
            }
        });

        btnEliminarPieza.setText("Eliminar Pieza");
        btnEliminarPieza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarPiezaActionPerformed(evt);
            }
        });

        txtPiezaSeleccionada.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAgregarPiezas)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnEliminarPieza))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtPiezaSeleccionada)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAtras)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSiguiente)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAgregarPiezas)
                    .addComponent(btnEliminarPieza))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSiguiente)
                    .addComponent(btnAtras)
                    .addComponent(txtPiezaSeleccionada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        // TODO add your handling code here:
        PantallaTrabajos pantT=new PantallaTrabajos();
        pantT.setMuebleActual(muebleActual);
        pantT.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnAgregarPiezasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarPiezasActionPerformed
        // TODO add your handling code here:
        PantallaNuevaPieza pantNP=new PantallaNuevaPieza(this, true);
        pantNP.setVisible(true);
    }//GEN-LAST:event_btnAgregarPiezasActionPerformed

    private void btnAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtrasActionPerformed
        // TODO add your handling code here:
        PantallaNombre pantN=new PantallaNombre();
        pantN.setMuebleActual(muebleActual);
        pantN.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnAtrasActionPerformed

    private void btnEliminarPiezaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarPiezaActionPerformed
        // TODO add your handling code here:
        if(txtPiezaSeleccionada.getText().compareTo("")==0)
        {
            JOptionPane.showMessageDialog(getContentPane(),"No se seleccionó ninguna Pieza","Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            Pieza piezaSeleccionada=null;
            if(muebleActual==null)
            {
                muebleActual=new MuebleEstructura();
            }
            LinkedList aux=muebleActual.getPartes();
            Iterator iter = aux.iterator();
            while(iter.hasNext())
            {
                Pieza aux2 = (Pieza)iter.next();
                if(aux2.getNombre().compareTo(txtPiezaSeleccionada.getText())==0)
                {
                    piezaSeleccionada=aux2;
                }
            }
            
            
            aux.remove(piezaSeleccionada);
            muebleActual.setPartes(aux);
            actualizarTablaPiezas();
        }
    }//GEN-LAST:event_btnEliminarPiezaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaPiezas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaPiezas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaPiezas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaPiezas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaPiezas().setVisible(true);
            }
        });
    }
    
    public void setMuebleActual(MuebleEstructura mueble)
    {
        muebleActual=mueble;
        if(muebleActual==null)
        {
            muebleActual=new MuebleEstructura();
        }
        actualizarTablaPiezas();
    }
    
    public void agregarNuevaPieza(Pieza nueva)
    {
        if(muebleActual==null)
        {
            muebleActual=new MuebleEstructura();
        }
        LinkedList aux=muebleActual.getPartes();
        aux.add(nueva);
        muebleActual.setPartes(aux);
        actualizarTablaPiezas();
    }
    
    private void actualizarTablaPiezas()
    {
        LinkedList listaPiezas=muebleActual.getPartes();
        String nombresColumnas[] = {"Piezas","Largo","Ancho","Espesor","Cantidad"};
        piezasTablaModel = new DefaultTableModel(nombresColumnas,0);
        Iterator iter = listaPiezas.iterator();
        while(iter.hasNext())
        {
            Pieza aux = (Pieza)iter.next();
            String datosFila[] = {aux.getNombre(),aux.getLargoCM()+"",aux.getAnchoCM()+"",aux.getEspesorMM()+"",aux.getCantidad()+""};
            piezasTablaModel.addRow(datosFila);
            
        }
        tablaPiezas.setModel(piezasTablaModel);
    }
    
    private void cargarMouseListener()
    {
        MouseAdapter adaptadorMouse=new MouseAdapter()
           {@Override
           public void mouseClicked(MouseEvent e) 
              {
                 int fila = tablaPiezas.rowAtPoint(e.getPoint());
                 int columna = tablaPiezas.columnAtPoint(e.getPoint());
                 if ((fila > -1) && (columna > -1))
                 {
                     txtPiezaSeleccionada.setText(tablaPiezas.getModel().getValueAt(fila, 0)+"");
                 }
              }
           };
        
        tablaPiezas.addMouseListener(adaptadorMouse);
    }
    
    private MuebleEstructura muebleActual;
    private DefaultTableModel piezasTablaModel;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarPiezas;
    private javax.swing.JButton btnAtras;
    private javax.swing.JButton btnEliminarPieza;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaPiezas;
    private javax.swing.JTextField txtPiezaSeleccionada;
    // End of variables declaration//GEN-END:variables
}
